home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / radio / recordlinear.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  161 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Record SGI audio data and output it as 8 kHz signed 16-bits samples,
  26.    converting the sampling rate and encoding on the fly.
  27.    This writes to stdout.
  28.    
  29.    Caveats:
  30.    - since it uses an audio port, of which there can only be 4 open
  31.      at a time, running several copies is not a good idea;
  32.      see broadcast.c and radio.c for a distribution mechanism.
  33.  
  34.   To do:
  35.   - support the other three sampling rates
  36. */
  37.  
  38. #include <stdio.h>
  39. #include <audio.h>
  40.  
  41. int inrate;
  42.  
  43. #define OUTRATE    8000
  44. #define INBUFSIZE 48000
  45.  
  46. main(argc, argv)
  47.     int argc;
  48.     char **argv;
  49. {
  50.     short inbuf[INBUFSIZE];
  51.     short outbuf[8000];
  52.     ALconfig c;
  53.     ALport p;
  54.     int n;
  55.     int insamps;
  56.     int sts = 0;
  57.     
  58.     checkinrate();
  59.     insamps = calcinsamps();
  60.     
  61.     c = ALnewconfig();
  62.     if (c == NULL) {
  63.         perror("ALnewconfig");
  64.         exit(1);
  65.     }
  66.     ALsetwidth(c, AL_SAMPLE_16);
  67.     ALsetchannels(c, AL_MONO);
  68.     
  69.     p = ALopenport(argv[0], "r", c);
  70.     if (p == NULL) {
  71.         perror("ALopenport");
  72.         exit(1);
  73.     }
  74.  
  75.     for (;;) {
  76.         checkinrate();
  77.         insamps = calcinsamps();
  78.         ALreadsamps(p, (void *)inbuf, insamps);
  79.         n = convert(inbuf, insamps, outbuf);
  80.         n = n * sizeof(short);
  81.         if (write(1, (char *)outbuf, n) != n) {
  82.             perror("write error");
  83.             sts = 1;
  84.             break;
  85.         }
  86.     }
  87.  
  88.     exit(sts);
  89. }
  90.  
  91. int calcinsamps()
  92. {
  93.     int insamps = inrate/2;
  94.     if (insamps > INBUFSIZE)
  95.         insamps = INBUFSIZE;
  96.     return insamps;
  97. }
  98.  
  99. checkinrate()
  100. {
  101.     inrate = getinrate();
  102.     if ((inrate/OUTRATE)*OUTRATE != inrate) {
  103.         fprintf(stderr,
  104.             "current sampling rate (%d) is not a multiple of %d\n",
  105.             inrate, OUTRATE);
  106.         exit(1);
  107.     }
  108. }
  109.  
  110. int getinrate()
  111. {
  112.     long PVbuffer[2];
  113.     
  114.     PVbuffer[0] = AL_INPUT_RATE;
  115.     ALgetparams(AL_DEFAULT_DEVICE, PVbuffer, 2);
  116.     return PVbuffer[1];
  117. }
  118.  
  119. int convert(inbuf, n, outbuf)
  120.     short *inbuf;
  121.     int n;
  122.     short *outbuf;
  123. {
  124.     register short *inp = inbuf;
  125.     register short *inend = inbuf + n;
  126.     register int di = inrate/OUTRATE;
  127.     register int x;
  128.     register short *outp = outbuf;
  129.  
  130.     while (inp < inend) {
  131.         switch (di) {
  132.         case 1:
  133.             *outp++ = *inp++;
  134.             break;
  135.         case 2:
  136.             x = *inp++;
  137.             x += *inp++;
  138.             *outp++ = x >> 1;
  139.             break;
  140.         case 4:
  141.             x = *inp++;
  142.             x += *inp++;
  143.             x += *inp++;
  144.             x += *inp++;
  145.             *outp++ = x >> 2;
  146.             break;
  147.         case 6:
  148.             x = *inp++;
  149.             x += *inp++;
  150.             x += *inp++;
  151.             x += *inp++;
  152.             x += *inp++;
  153.             x += *inp++;
  154.             *outp++ = x / 24;
  155.             break;
  156.         }
  157.     }
  158.  
  159.     return outp - outbuf;
  160. }
  161.